/*
 * 线性代数算法库
 */
define(["require", "exports"], function (require, exports) {
    function LinearAlgebra() {

    }

    
    /*
     * 求两矢量旋转角度
     * vector1，vector2 表示矢量 参数结构：[x,y] 
     * clockwise 表示是否是顺时针旋转 参数结构：true(顺时)，false(逆时)，默认为顺时
     * 返回值为float 表示为旋转弧度
     */
    LinearAlgebra.prototype.GetVectorsRotateAngle = function(vector1,vector2,clockwise){
    	 var  nyPI = Math.acos(-1.0);
    	 var x1 = vector1[0];
    	 var y1 = vector1[1];
    	 var x2 = vector2[0];
    	 var y2 = vector2[1]
    	 var dist1 = Math.sqrt( x1 * x1 + y1 * y1 );
         var dist2 = Math.sqrt( x2 * x2 + y2 * y2 );
         // 点乘
         var dot = x1 * x2 + y1 * y2;
         var angle = Math.acos(dot/(dist1*dist2));
         //叉乘
         var cross = x1*y2 - x2*y1;
         if(cross<0)
         angle = 2*nyPI - angle;
         if(clockwise==null || clockwise==true)
            return angle;
         else
            return (2*nyPI - angle);
         
    }
    
    
    /*
     * 获取两条线的交点
     * line1 ,line2 表示判断的两条线 数据结构：[[x1,y1],[x2,y2]] 和[[X1,Y1],[X2,Y2]]
     * islimit1,islimit2 表示判断两条线是线段还是直线，true是线段，输入的坐标就是它两个节点，false则为直线
     */
    LinearAlgebra.prototype.LineLineRel = function(line1,line2,islimit1,islimit2){
    	var p1 = line1[0];
    	var p2 = line1[1];
    	var t1 = line2[0];
    	var t2 = line2[1];
    	var result = {"rel":"","coordinate":[]};
    	//两条线的斜率
    	var k1 = (p1[1]-p2[1])/(p1[0]-p2[0]);
    	var k2 = (t1[1]-t2[1])/(t1[0]-t2[0]);
    	
    	if(Math.abs(k1) == Infinity && Math.abs(k2)!=Infinity){
    		var b2 = t1[1]-k2*t1[0];
    		var x = p1[0];
    		var y = k2*x+b2;
    		//设置值的区间
    		var insertX = [];
    		var limit_x = [null,null];
    		if(islimit1==true){
    			insertX.push(p1[0]);
    			insertX.push(p2[0]);
    		}
    		if(islimit2==true){
    			insertX.push(t1[0]);
    			insertX.push(t2[0]);
    		}
    		insertX.sort();
    		limit_x=[insertX[0],insertX[insertX.length-1]];
    		limit_x = limit_x.filter(function(item){return item!=null});
    		if(limit_x.length!=0 &&(x<limit_x[0]||x>limit_x[1]))
    		    result = {"rel":"不相交","coordinate":[]}
    		else
    		    result = {"rel":"相交","coordinate":[x,y]};
    	}
    	else if(Math.abs(k2) == Infinity && Math.abs(k1)!=Infinity){
    		var b1 = p1[1]-k1*p1[0];
    		var x = t1[0];
    		var y = k1*x+b1;
    		//设置值的区间
    		var insertX = [];
    		var limit_x = [null,null];
    		if(islimit1==true){
    			insertX.push(p1[0]);
    			insertX.push(p2[0]);
    		}
    		if(islimit2==true){
    			insertX.push(t1[0]);
    			insertX.push(t2[0]);
    		}
    		insertX.sort();
    		limit_x=[insertX[0],insertX[insertX.length-1]];
    		limit_x = limit_x.filter(function(item){return item!=null});
    		if(limit_x.length!=0 &&(x<limit_x[0]||x>limit_x[1]))
    		    result = {"rel":"不相交","coordinate":[]}
    		else
    		    result = {"rel":"相交","coordinate":[x,y]};
    	}
    	else if(Math.abs(k2) == Infinity && Math.abs(k1)==Infinity){
    		var x1 = p1[0];
    		var x2 = t1[0];
    		if(x1!=x2){
    			result = {"rel":"不相交","coordinate":[]};
    		}
    		else{
    			if(islimit1==false || islimit2 == false)
    			{
    				result = result = {"rel":"重合","coordinate":[]};
    			}
    			else{
    			    var limit_y1 = [p1[1],p2[1]].sort();
    			    var limit_y2 = [t1[1],t2[1]].sort();
    			    if(limit_y1[1]<limit_y2[0])
    		            result = {"rel":"不相交","coordinate":[]};
    			    if(limit_y1[1]==limit_y2[0])
    		            result = {"rel":"相交","coordinate":[x1,limit_y1[1]]};
    		        if(limit_y1[1]>limit_y2[0]&&limit_y1[1]<limit_y2[1])
    		            result = {"rel":"重合","coordinate":[]};
    		        if(limit_y1[0]>limit_y2[0]&&limit_y1[0]<limit_y2[1])
    		            result = {"rel":"重合","coordinate":[]};
    		        if(limit_y1[0]==limit_y2[1])
    		            result = {"rel":"相交","coordinate":[x1,limit_y1[0]]};
    		        if(limit_y1[0]>limit_y2[1])
    		            result = {"rel":"不相交","coordinate":[]};
    		    }
    		}
    	}
    	else if(Math.abs(k1) != Infinity && Math.abs(k2) != Infinity){
    		var b1 = p1[1]-k1*p1[0];
    		var b2 = t1[1]-k2*t1[0];
    		var x = (b2-b1)/(k1-k2);
    		if(Math.abs(x)==Infinity)
    		{
    			if(b1!=b2)
    			    result = {"rel":"不相交","coordinate":[]}
    			else
    			{
    				if(islimit1==false || islimit2 == false)
    			    {
    				    result ={"rel":"重合","coordinate":[]};
    			    }
    			    else{
    				    var limit_x1 = [p1[0],p2[0]].sort();
    			        var limit_x2 = [t1[0],t2[0]].sort();
    			        if(limit_x1[1]<limit_x2[0])
    		                result = {"rel":"不相交","coordinate":[]};
    			        if(limit_x1[1]==limit_x2[0])
    		                result = {"rel":"相交","coordinate":[limit_x1[1],k1*limit_x1[1]+b1]};
    		            if(limit_x1[1]>limit_x2[0]&&limit_x1[1]<limit_x2[1])
    		                result = {"rel":"重合","coordinate":[]};
    		            if(limit_x1[0]>limit_x2[0]&&limit_x1[0]<limit_x2[1])
    		                result = {"rel":"重合","coordinate":[]};
    		            if(limit_x1[0]==limit_x2[1])
    		                result = {"rel":"相交","coordinate":[limit_x1[0],k1*limit_x1[0]+b1]};
    		            if(limit_x1[0]>limit_x2[1])
    		                result = {"rel":"不相交","coordinate":[]};
    		        }
    			}
    		}
    		else{
    			var y = k1*x+b1;
    			//设置值的区间
    		    var insertX = [];
    		    var limit_x = [null,null];
    		    if(islimit1==true){
    			    insertX.push(p1[0]);
    			    insertX.push(p2[0]);
    		    }
    		    if(islimit2==true){
    			    insertX.push(t1[0]);
    			    insertX.push(t2[0]);
    		    }
    		    insertX.sort();
    		    limit_x = [insertX[0],insertX[insertX.length-1]];
    		    limit_x = limit_x.filter(function(item){return item!=null});
    		    if(limit_x.length!=0 &&(x<limit_x[0]||x>limit_x[1]))
    		        result = {"rel":"不相交","coordinate":[]}
    		    else
    		        result = {"rel":"相交","coordinate":[x,y]};
    		}
    	}
    	return result;
    }
    
      return LinearAlgebra;
});
